package userlogic

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/user"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetBalanceLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetBalanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBalanceLogic {
	return &GetBalanceLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetBalance 获取余额
func (l *GetBalanceLogic) GetBalance(in *rpc.GetBalanceRequest) (*rpc.GetBalanceResponse, error) {
	// 初始化模型
	walletModel := user.NewWalletModel(l.ctx, l.svcCtx.Db)
	// 获取用户余额
	wallet, err := walletModel.GetBalance(in.UserId)
	if err != nil {
		return nil, err
	}
	// 数据映射
	return &rpc.GetBalanceResponse{
		UserId:        in.UserId,
		Balance:       wallet.Balance,
		TotalRecharge: wallet.TotalRecharge,
		TotalWithdraw: wallet.TotalWithdraw,
	}, nil
}
